﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
    xmlns:conv="clr-namespace:SqlMiM.Common.Converters"
    xmlns:vm="clr-namespace:SqlMiM.Common.ViewModel"
    xmlns:c="clr-namespace:SqlMiM.Common.Controls"
    xmlns:a="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:uc="clr-namespace:SqlMiM.Common.UserControls" 
    x:Class="SqlMiM.Common.View.ServerLoginView"
    Height="300"
    MinHeight="300"
    Width="300"
    MinWidth="300"
    Background="#303030"
    FontSize="7.5pt"
    FontFamily="Verdana"
    Foreground="#DDD">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SqlMiM.Common;component/Resources/ControlsTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <conv:ReverseBooleanConverter x:Key="ReverseBooleanConverter" />
            
            
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

            <ControlTemplate x:Key="ServerComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="20" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.ColumnSpan="2">
                        <Border x:Name="Bg" CornerRadius="3" BorderBrush="{DynamicResource BrushRaisedColorHighLight}" BorderThickness="0,1,0,0">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Offset="0.0" Color="#585858" />
                                    <GradientStop Offset="1.2" Color="#282828" />
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <Border x:Name="Dn" Opacity="0" BorderThickness="0,1,0,1" CornerRadius="3" BorderBrush="#282828" >
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Offset="0.0" Color="#505050" />
                                    <GradientStop Offset="1.2" Color="#383838" />
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <Border x:Name="Sh" BorderBrush="{DynamicResource BrushRaisedShadow}" BorderThickness="0,0,0,1" CornerRadius="3" />

                        <ContentPresenter x:Name="Content" Margin="0,0,0,2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                    </Grid>
                    <Image Source="/SqlMiM.Common;component/Images/database_refresh.png" Cursor="Hand" Width="16" x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"  />
                    <!--<Path Fill="{TemplateBinding Foreground}"  Data="M 0 0 L 4 4 L 8 0 Z"/>-->
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#888888" Duration="0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" To="#686868" Duration="0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" To="#282828" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Duration="0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" Duration="0:0:0.2" />
                                    <ColorAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource BrushDisabledForeground}"/>
                        <Setter Property="Opacity" Value="0.75"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

            <Style x:Key="ServerCombobox" TargetType="{x:Type ComboBox}">

                <!--<Setter Property="SnapsToDevicePixels" Value="true"/>-->
                <!--<Setter Property="OverridesDefaultStyle" Value="true"/>-->
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
                <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
                <Setter Property="Foreground" Value="{DynamicResource Foreground}"/>
                <Setter Property="MinHeight" Value="20"/>
                <Setter Property="MinWidth" Value="70"/>


                <!---->
                
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBox}">
                            <Grid>
                                <ToggleButton MinWidth="0"  Name="ToggleButton" 
                                        Template="{DynamicResource ServerComboBoxToggleButton}"
                                        Foreground="{TemplateBinding Foreground}" Grid.Column="2" Focusable="false"
                                        Command="{Binding LoadServersCommand}"
                                        IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                        ClickMode="Press" >
                                   
                                </ToggleButton>
                                
                                <ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                          Margin="6,0,23,2" VerticalAlignment="Center" HorizontalAlignment="Left" />
                                
                                <TextBox x:Name="PART_EditableTextBox" HorizontalAlignment="Stretch" Margin="3,0,23,3" VerticalAlignment="Center"
                                         Background="Transparent" Focusable="True" Foreground="{TemplateBinding Foreground}"
                                         IsReadOnly="{TemplateBinding IsReadOnly}" Style="{x:Null}" Template="{DynamicResource ComboBoxTextBox}"/>
                                
                                <Popup Name="Popup" Placement="Bottom" VerticalOffset="0" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                                    <a:SystemDropShadowChrome Margin="0,0,5,5" CornerRadius="0">
                                        <Grid>

                                            <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" Name="DropDown" SnapsToDevicePixels="True">
                                                <Border Background="#FF585858" CornerRadius="4,4,4,4" Name="DropDownBorder" />
                                                <ScrollViewer SnapsToDevicePixels="True">
                                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                                </ScrollViewer>
                                            </Grid>
                                            <Grid Name="DropDownNoItems" SnapsToDevicePixels="True" 
                                                  MinWidth="{TemplateBinding ActualWidth}" Height="30" >
                                                <Border x:Name="DropDownBorderNoItems" Background="#585858" CornerRadius="4" />

                                                <uc:Waiter x:Name="Waiter" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Width="20" Height="20" />

                                                <TextBlock Text="Loading servers..." Margin="30,0,0,0" VerticalAlignment="Center" Name="LoadingState" />
                                            </Grid>

                                        </Grid>
                                    </a:SystemDropShadowChrome>
                                </Popup>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                                </Trigger>
                                
                                <Trigger Property="c:ComboboxExtender.IsLoadingServers" Value="true" >

                                    <Setter TargetName="DropDown" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="DropDownNoItems" Property="Visibility" Value="Visible" />
                                    <Setter TargetName="Popup" Property="StaysOpen" Value="true" />
                                    <Setter TargetName="Popup" Property="IsOpen" Value="true" />
                                    <Setter TargetName="ToggleButton" Property="IsEnabled" Value="false" />

                                </Trigger>
                                <Trigger Property="c:ComboboxExtender.IsLoadedServers" Value="true" >


                                    <Setter TargetName="DropDown" Property="Visibility" Value="Visible"/>
                                    <Setter TargetName="DropDownNoItems" Property="Visibility" Value="Collapsed" />

                                </Trigger>

                                <Trigger Property="c:ComboboxExtender.NoServersFound" Value="true" >

                                    <Setter TargetName="DropDown" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="DropDownNoItems" Property="Visibility" Value="Visible" />

                                    <Setter TargetName="LoadingState" Property="Text" Value="No Servers found." />

                                    <Setter TargetName="Waiter" Property="Visibility" Value="Hidden" />
                                </Trigger>

                                <Trigger Property="HasItems" Value="true">
                                    <Setter TargetName="DropDownNoItems" Property="Visibility" Value="Collapsed" />
                                    <Setter TargetName="DropDown" Property="Visibility" Value="Visible"/>
                                </Trigger>
                                
                                <Trigger Property="IsEnabled" Value="false">
                                </Trigger>
                                
                                <Trigger Property="IsGrouping" Value="true">
                                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                </Style.Triggers>
            </Style>

            <DataTemplate x:Key="ServerIdentityTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" Margin="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
                </StackPanel>
            </DataTemplate>


            <CollectionViewSource x:Key="cvsServersIdentities" Source="{Binding Path=ServerIdentities, Mode=TwoWay}" />
        </ResourceDictionary>

    </UserControl.Resources>

    <Grid>

        <!--Server Name-->
        <DockPanel LastChildFill="True" Margin="10,12,0,0" VerticalAlignment="Top">

            <TextBlock Margin="0,0,0,0"  x:Name="label1" VerticalAlignment="Center" FontWeight="Bold" Text="Server Name :" />

            <ComboBox x:Name="cbx" Margin="10,0,10,0" VerticalAlignment="Center" IsEditable="True" Style="{DynamicResource ServerCombobox}"
                      ItemTemplate="{DynamicResource ServerIdentityTemplate}" TextSearch.TextPath="Name"
                      Text="{Binding Path=ServerName, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" 
                      c:ComboboxExtender.IsLoadedServers="{Binding IsLoadedServers}" 
                      c:ComboboxExtender.IsLoadingServers="{Binding IsLoadingServers}" 
                      c:ComboboxExtender.NoServersFound="{Binding NoServersFound}" >
                <ComboBox.ItemsSource>
                    <Binding Path="ServerIdentitiesView.View" UpdateSourceTrigger="PropertyChanged" />
                </ComboBox.ItemsSource>
            </ComboBox>

        </DockPanel>

        <!--Authentication Mode-->
        <TextBlock  HorizontalAlignment="Left" Margin="12,50,0,0" x:Name="label2" VerticalAlignment="Top" FontWeight="Bold" Text="Authentication :"/>

        <StackPanel Margin="10,80,10,0" Orientation="Vertical" VerticalAlignment="Top">
            <RadioButton Margin="50,0,0,0" GroupName="AuthenticationMode" x:Name="rbtWindowsAuthentication" Content="Use Windows NT Integrated Security" 
                         IsChecked="{Binding Path=IsSqlServerAuthenticationMode, Converter={StaticResource ReverseBooleanConverter}, Mode=TwoWay}" />

            <RadioButton Margin="50,10,0,0" GroupName="AuthenticationMode" x:Name="rbtSqlAuthentication" Content="Use a specific User ID and Password" 
                         IsChecked="{Binding Path=IsSqlServerAuthenticationMode, Mode=TwoWay}" />
        </StackPanel>

        <!--User Id And Password for Sql Server Authentication-->
        <Grid x:Name="grid" Margin="10,130,10,0"  VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="160" />
            </Grid.ColumnDefinitions>


            <!--User Id-->
            <TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,10,0" HorizontalAlignment="Right"  VerticalAlignment="Center" 
                   IsEnabled="{Binding Path=IsChecked, ElementName=rbtSqlAuthentication}" Text="User ID :" />
            <TextBox  Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" 
                      Text="{Binding Path=UserName, Mode=TwoWay}"
                      IsEnabled="{Binding Path=IsChecked, ElementName=rbtSqlAuthentication}"/>

            <!--Password-->
            <TextBlock Grid.Row="1" Grid.Column="0"  Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center"
                   IsEnabled="{Binding Path=IsChecked, ElementName=rbtSqlAuthentication}" Text="Password :" />
            <TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"
                     Text="{Binding Path=Password, Mode=TwoWay}" 
                     IsEnabled="{Binding Path=IsChecked, ElementName=rbtSqlAuthentication}" />
        </Grid>

        <DockPanel LastChildFill="True" Height="30" Margin="10,200,10,0" VerticalAlignment="Top">

            <!--Connection Timeout :-->
            <TextBlock Margin="0,0,0,0" DockPanel.Dock="Left" VerticalAlignment="Center" FontWeight="Bold" Text="Connection Timeout :" />

            <TextBlock Margin="0,0,0,0" DockPanel.Dock="Right" VerticalAlignment="Center" Text="secs." />
            <TextBlock Margin="0,0,5,0" DockPanel.Dock="Right" VerticalAlignment="Center" Text="{Binding Path=ConnectionTimeOut, Mode=OneWay}" />
            <Slider Margin="10,0"  x:Name="slider1" VerticalAlignment="Center" Minimum="5" Maximum="60" 
                    Value="{Binding Path=ConnectionTimeOut, Mode=TwoWay}" />
        </DockPanel>

        <UniformGrid  Margin="0,0,10,10" Columns="2" Rows="1" Height="42" Width="Auto" VerticalAlignment="Bottom" HorizontalAlignment="Right">
            <!--Buttons Cancel - Connect-->
            <Button Height="25" IsDefault="True" Margin="10,0,10,0" Content="Connect" Command="{Binding Path=ValidateCommand}" Name="ConnectButton" Click="ConnectButton_Click" />
            <Button Height="25" IsCancel="True" Margin="10,0,10,0" Content="Cancel" Command="{Binding CancelCommand}"  Name="CancelButton" />

        </UniformGrid>

        <uc:Waiter HorizontalAlignment="Left" Margin="50,0,0,20" VerticalAlignment="Bottom" Width="20" Height="20"
                                    Visibility="{Binding IsConnecting, Converter={StaticResource BooleanToVisibilityConverter}}" />



    </Grid>

</UserControl>
